package com.ltv.xiangqibook.common;

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class XiangQiBookConfiguration {
    public static final String CONFIG_FILE_NAME = "conf/xianqibook.conf";
    
    private static final String DB_HOST = "db_host";
    private static final String DB_PORT = "db_port";
    private static final String DB_NAME = "db_name";
    private static final String DB_USER = "db_user";
    private static final String DB_PASSWORD = "db_password";
    private static final String BOOK_LOCAL_LOCATION = "book_local_location";
    private static final String CONNECTION_POOL_SIZE = "connection_pool_size";
    private static final String WAKE_UP_PERIOD = "wake_up_period";
    private static final String WAKE_UP_QUERY = "wake_up_query";
    
    private static final String PLUGIN_VERSION = "plugin_version";
    
    private static final int DEFAULT_CONNECTION_POOL_SIZE = 10;
    private static final int DEFAULT_WAKE_UP_PERIOD = 4*60; // 240 minutes
    private static final String DEFAULT_WAKE_UP_QUERY = "SELECT 1";
    
    private Properties props;
    
    private static XiangQiBookConfiguration instance;
    
    private XiangQiBookConfiguration() {
        props = new Properties();
    }

    public static XiangQiBookConfiguration getInstance() {
        if (instance == null) {
            instance = new XiangQiBookConfiguration();
        }
        return instance;
    }
    
    public String getDBHost() {
        return props.getProperty(DB_HOST);
    }
    
    public String getDBPort() {
        return props.getProperty(DB_PORT);
    }
    
    public String getDBName() {
        return props.getProperty(DB_NAME);
    }
    
    public String getDBUser() {
        return props.getProperty(DB_USER);
    }
    
    public String getDBPassword() {
        return props.getProperty(DB_PASSWORD);
    }
    
    public String getBookLocalLocation() {
        return props.getProperty(BOOK_LOCAL_LOCATION);
    }
    
    public int getConnectionPoolSize() {
        int ret = DEFAULT_CONNECTION_POOL_SIZE;
        try {
            ret = Integer.parseInt(props.getProperty(CONNECTION_POOL_SIZE));
        } catch (Exception e) {
            ret = DEFAULT_CONNECTION_POOL_SIZE;
        }
        
        return ret;
    }
    
    public String getPluginVersion() {
        return props.getProperty(PLUGIN_VERSION);
    }
    
    public int getWakeUpPeriod() {
    	int ret = DEFAULT_WAKE_UP_PERIOD;
        try {
            ret = Integer.parseInt(props.getProperty(WAKE_UP_PERIOD));
        } catch (Exception e) {
            ret = DEFAULT_WAKE_UP_PERIOD;
        }
        
        return ret;
    }
    
    public String getWakeUpQuery() {
    	String ret = props.getProperty(WAKE_UP_QUERY);
    	if (ret == null || ret.trim().equals("")) {
    		ret = DEFAULT_WAKE_UP_QUERY;
    	}
    	
    	return ret;
    }
    
    public void loadConfiguration(String fileName) throws Exception {
        FileInputStream fi = null;
        InputStreamReader isr = null;
        try {
            fi = new FileInputStream(fileName);
            isr = new InputStreamReader(fi, "utf-8");
            props.load(isr);
        } finally {
            if (fi != null) {
                fi.close();
            }
            if (isr != null) {
                isr.close();
            }
        }
    }
    
}
